package com.diors.commons.utils;

import java.io.StringWriter;

import org.codehaus.jackson.map.ObjectMapper;

/**
 * 对象JSON字符串工具类
 * @author	darren.ouyang <ouyang.darren@gmail.com>
 * @date	2013-10-28
 * @version 1.0
 * @copyright Copyright (c) 2013, darren.ouyang
 */
public class JsonObjectUtil 
{

	private static ObjectMapper obj = new ObjectMapper();
	/**
	 * 对象转换成json
	 * @return
	 */
	public static String objectToJson(Object object)
	{
		
		StringWriter sw = new StringWriter();
		try 
		{
			obj.writeValue(sw, object);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		
		return sw.toString();
	}
	
	public static <T extends Object>T jsonToObject(String str, Class<T> clazz)
	{
		
		T t = null;
		try
		{
			t = obj.readValue(str, clazz);
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return t;
	}
}
